<template>
    <div>
        <Form :label-width="100" inline>
            <Form-item label="发放年月">
                <DatePicker type="month" style="width:150px" v-model="queryParam.acay0003"
                            placeholder="请选择发放年月"  :editable="false"></DatePicker>
            </Form-item>
            <Form-item label="救助类型">
                <Select style="width:150px" v-model="queryParam.acay0002" clearable>
                    <Option value='1010'> 城市低保对象</Option>
                    <Option value='1030'> 农村低保对象</Option>
                    <Option value='1020'> 城市低保边缘家庭</Option>
                    <Option value='1040'> 农村低保边缘家庭</Option>
                </Select>
            </Form-item>
            <FormItem label="是否生成数据">
                <dictSelect :sel_val='queryParam.acay0006' dictCode="yesorno" :bindData.sync="queryParam.acay0006"
                            style="width: 150px" clearable/>
            </FormItem>
            <FormItem label="是否确认">
                <dictSelect :sel_val='queryParam.acay0008' dictCode="yesorno" :bindData.sync="queryParam.acay0008"
                            style="width: 150px" clearable/>
            </FormItem>
            <Row class="button_row">
                <span style="float: left;">
                <Button type="primary" @click="handleAdd()">录入</Button>
                </span>
                <span style="float: right;">
                      <Button type="primary" @click="searchQuery">查询</Button>
                <Button type="primary" class="button_item_left" @click="searchReset">重置</Button>
                </span>
            </Row>
        </Form>
        <Modal v-model="visible" :title="title" width="900" style="position:relative;">
            <Form ref="formData" :model="formData" :label-width="160" :rules="rules">
                <FormItem label="功能说明：">
                     <span><font color="black">
                    	1、根据【低保花名册提取年月】和【救助类型】提取花名册数据(该月份花名册已确认)进行两节救助数据的生成。<br/>
                    	2、【救助类型】:城市低保包含城市最低生活保障、城市单人保、城市因病致贫保，农村低保包含农村最低生活保障、农村单人保、农村因病致贫保、农村建档立卡。<br/>
                    	3、【两节救助时间】和【低保花名册提取年月】只能选择当前月度或以后月度，不能选择历史时间。<br/>
                    	4、如果是本年第二批次两节数据，系统自动选定低保提取月度：第一批次低保年月延后一个月。<br/>
                    	5、第二批次两节数据生成时会根据保障家庭户主姓名、身份证号码自动排除在第一次批次中已存在的家庭。
                    	</font>
                    </span>
                </FormItem>

                <FormItem label="救助类型：" prop="acay0002">
                    <Select style="width:400px" v-model="formData.acay0002" :disabled="!showSubmit" clearable>
                        <Option value='1010'> 城市低保对象</Option>
                        <Option value='1030'> 农村低保对象</Option>
                        <Option value='1020'> 城市低保边缘家庭</Option>
                        <Option value='1040'> 农村低保边缘家庭</Option>
                    </Select>
                </FormItem>
                <FormItem label="是否为本年度第二批两节数据:">
                    <RadioGroup v-model="vIsSecond" @on-change="checkData">
                        <Radio :label="0">否</Radio>
                        <Radio :label="1">是</Radio>
                    </RadioGroup>
                </FormItem>
                <FormItem label="花名册提取年月：" prop="acay0001">
                    <DatePicker type="month" :options="options3" v-model="formData.acay0001" placeholder="请选择低保花名册提取年月"
                                style="width: 400px" :disabled="!showSubmit"  :editable="false"></DatePicker>
                </FormItem>
                <FormItem label="两节救助时间：" prop="acay0004">
                    <DatePicker type="date" :options="options1" v-model="formData.acay0004" placeholder="请选择两节救助时间"
                                style="width: 400px" :disabled="!showSubmit"  :editable="false"></DatePicker>
                </FormItem>
                <FormItem label="两节救助标准：" prop="acay0010">
                    <dictSelect :sel_val='formData.acay0010' dicType="service" style="width: 400px"
                                :bindData.sync="formData.acay0010"
                                dictService="/api/ac/subsistence/ss06007Service/getSelectList"
                                class="itemStyle"/>
                </FormItem>
                <FormItem label="备注：">
                    <Input type="textarea" v-model="formData.acay0005" placeholder="" clearable style="width: 300px"
                           :disabled="!showSubmit"></Input>
                </FormItem>

            </Form>
            <div slot="footer">
                <Button @click="visible = false">关闭</Button>
                <Button type="primary" @click="handleSubmit('formData')" v-if="showSubmit">确定</Button>
            </div>
        </Modal>
        <div class="table_div">
            <Table :columns="columns" :data="tableData" :loading="Loading">
                <template slot-scope="{ row, index  }" slot="action">
                    <Button type="info" size="small" style="margin-right: 5px" @click="handleCreate(index)">数据生成
                    </Button>
                    <Button type="info" size="small" style="margin-right: 5px" @click="handleConfirm(index)">数据确认
                    </Button>
                    <!--                    <Button type="info" size="small" style="margin-right: 5px" @click="handleEdit(index)">编辑</Button>-->
                    <Button type="error" size="small" style="margin-right: 5px" @click="handleDelete(index)">删除</Button>
                    <Button type="success" size="small" style="margin-right: 5px" @click="handleView(index)">查看</Button>
                </template>
            </Table>
            <Page :total="dataCount" :page-size="pageSize" show-total class="paging"
                  @on-change="handleTableChange" :current="pageNo"></Page>
        </div>
    </div>
</template>

<script>
    import {postAction, getAction} from '@/api/request'
    import {CommonMixin} from '@/views/common/js/CommonMixin'
    import dictSelect from '@/components/dictSelect/dictSelect.vue'
    import DateFormat from '@/plugins/format.js'

    export default {
        mixins: [CommonMixin],
        components: {
            dictSelect
        },
        data() {
            return {
                vIsSecond: 0,
                options3: {
                    disabledDate(date) {
                        let dateTime = new Date();
                        let currentYear = dateTime.getFullYear();
                        let currentMonth = dateTime.getMonth();
                        let monthFirstDay = new Date(currentYear, currentMonth, 1);

                        let newMonth = ++currentMonth;
                        let newYear = currentYear;
                        if (newMonth >= 12) {
                            newMonth -= 12;
                            newYear++;
                        }
                        let nextMonthFirst = new Date(newYear, newMonth, 1);
                        let nonthLastDay = new Date(
                            nextMonthFirst.getTime() - 24 * 60 * 60 * 1000
                        );
                        return (
                            date.valueOf() < new Date(monthFirstDay).getTime()
                            //截止月份 || date.valueOf() > new Date(nonthLastDay).getTime()
                        );
                    }
                },
                options1: {
                    disabledDate(date) {
                        //当前日期的之前的日期不可选
                        return date && date.valueOf() < Date.now() - 86400000;
                    }
                },
                rules: {
                    acay0002: [
                        {required: true, message: '救助类型必填', trigger: 'blur'}
                    ],
                    acay0010: [
                        {required: true, message: '请选择"两节救助标准"', trigger: 'blur'}
                    ],
                    acay0004: [
                        {required: true, message: '请选择"两节救助时间"', trigger: 'blur,change', type: 'date'}
                    ],
                    acay0001: [
                        {required: true, message: '请选择"低保花名册提取年月"', trigger: 'blur,change', type: 'date'}
                    ]
                },
                columns: [
                    {
                        title: '发放年月',
                        key: 'acay0003',
                        align: "center"
                    },
                    {
                        title: '区县名称',
                        key: 'acay0007_name',
                        align: "center"
                    },
                    {
                        title: '花名册提取年月',
                        key: 'acay0001',
                        align: "center"
                    },
                    {
                        title: '救助对象类型',
                        key: 'acay0002_name',
                        width: '150px',
                        align: "center"
                    },
                    {
                        title: '是否生成数据',
                        width: '100px',
                        key: 'acay0006_name',
                        align: "center"
                    },
                    {
                        title: '是否确认',
                        key: 'acay0008_name',
                        width: '100px',
                        align: "center"
                    },
                    {
                        title: '救助时间',
                        key: 'acay0004',
                        width: '110px',
                        align: "center"
                    },
                    {
                        title: '操作',
                        key: 'action',
                        align: 'center',
                        slot: 'action',
                        width: 350,
                        fixed: 'right',
                    }
                ],
                tableData: [],
                Loading: false,
                url: {
                    list: "/api/ac/subsistence/ss02060Service/getPageList",
                    edit: "/api/ac/subsistence/ss02060Service/updateSs02060",
                    add: "/api/ac/subsistence/ss02060Service/insertSs02060",
                    createData: "/api/ac/subsistence/ss02060Service/createData",
                    dataConfirm: "/api/ac/subsistence/ss02060Service/insertDataConfirm",
                    delete: "/api/ac/subsistence/ss02060Service/deleteSs02060",
                    checkSecondBatch: "/api/ac/subsistence/ss02060Service/checkSecondBatch",
                },
                formData: {
                    acay0001: '',
                    acay0002: '',
                    acay0003: '',
                    acay0004: '',
                    acay0005: '',
                    acay0006: '',
                    acay0007: '',
                    acay0008: '',
                    acay0009: '',
                    acay0010: '',
                },
                queryParam: {},
                visible: false,
                showSubmit: true,
                dataCount: 0,
                pageSize: 10,
                modal: false,
                title: ""
            }
        },
        methods: {
            checkData(type) {
                let that = this;
                if (type == '1') {
                    that.vIsSecond = 1;
                    let acay0002 = this.formData.acay0002;
                    if (acay0002 == undefined) {
                        that.vIsSecond = 0;
                        alert("F:" + that.vIsSecond)
                        that.$Message.error('请选择救助类型');
                        //radio 选中 否
                        return;
                    }
                    let url = that.url.checkSecondBatch;
                    let dataParam = {};
                    dataParam.acay0002 = acay0002;
                    postAction(url, dataParam).then((res) => {
                        if (res.data.errcode != 0) {
                            that.$Message.error(res.data.errmsg)
                        } else {
                            that.$Message.success(res.data.data.msg);
                            if (res.data.data.data.acay0001 != undefined) {
                                that.formData.acay0001 = res.data.data.data.acay0001;
                                that.formData.acay0009 = res.data.data.data.acay0009;
                            }
                        }
                        that.Loading = false;
                    });
                } else {
                    that.vIsSecond = 0;
                }
            },
            handleConfirm(index) {
                //数据确认
                //数据生成
                let that = this;
                let record = that.tableData[index];
                if (record.acay0008 == '1') {
                    this.$Message.error("已经确认两节救助数据，不允许重复数据确认！");
                    return;
                } else if (record.acay0006 == '0') {
                    this.$Message.error("未生成两节救助数据，不允许进行数据确认！");
                    return;
                } else if (record.acay0006 == '1') {
                    that.$Modal.confirm({
                        title: '确认两节救助数据后，不允许重复生成本月数据！确定当前操作吗？',
                        onOk: () => {
                            that.Loading = true;
                            let url = that.url.dataConfirm;
                            let dataParam = {};
                            dataParam.id = record.id;
                            postAction(url, dataParam).then((res) => {
                                if (res.data.errcode != 0) {
                                    that.$Message.error(res.data.errmsg)
                                } else {
                                    that.$Message.success(res.data.data.msg);
                                    that.loadData(1)
                                }
                                that.Loading = false;
                            });
                        },
                        onCancel: () => {
                        }
                    });
                } else {
                    that.$Modal.confirm({
                        title: '确定生成两节救助数据吗？',
                        onOk: () => {
                            that.Loading = true;
                            let url = that.url.createData;
                            let dataParam = {};
                            dataParam.id = record.id;
                            postAction(url, dataParam).then((res) => {
                                if (res.data.errcode != 0) {
                                    that.$Message.error(res.data.errmsg)
                                } else {
                                    that.$Message.success(res.data.data.msg);
                                    that.loadData(1)
                                }
                                that.Loading = false;
                            });
                        },
                        onCancel: () => {
                        }
                    });
                }
            },
            handleCreate(index) {
                //数据生成
                let that = this;
                let record = that.tableData[index];
                if (record.acay0008 == '1') {
                    this.$Message.error("已经确认两节救助数据，不允许重复生成！");
                    return;
                } else if (record.acay0006 == '1') {
                    that.$Modal.confirm({
                        title: '已经生成数据,确定重新生成数据吗？',
                        onOk: () => {
                            that.Loading = true;
                            let url = that.url.createData;
                            let dataParam = {};
                            dataParam.id = record.id;
                            postAction(url, dataParam).then((res) => {
                                if (res.data.errcode != 0) {
                                    that.$Message.error(res.data.errmsg)
                                } else {
                                    that.$Message.success(res.data.data.msg);
                                    that.loadData(1)
                                }
                                that.Loading = false;
                            });
                        },
                        onCancel: () => {
                        }
                    });
                } else {
                    that.$Modal.confirm({
                        title: '确定生成两节救助数据吗？',
                        onOk: () => {
                            that.Loading = true;
                            let url = that.url.createData;
                            let dataParam = {};
                            dataParam.id = record.id;
                            postAction(url, dataParam).then((res) => {
                                if (res.data.errcode != 0) {
                                    that.$Message.error(res.data.errmsg)
                                } else {
                                    that.$Message.success(res.data.data.msg);
                                    that.loadData(1)
                                }
                                that.Loading = false;
                            });
                        },
                        onCancel: () => {
                        }
                    });
                }
            },
            searchQueryNew() {
                this.queryParam.cfag0004 = this.DateFormat(this.queryParam.cfag0004, 'YYYY');
                this.loadData();
            },
            handleAdd() {
                this.$refs.formData.resetFields()
                let that = this;
                that.title = "数据录入"
                let record = {};
                that.visible = true
                that.showSubmit = true
                that.formData = Object.assign({}, record)
            },
            handleDelete(index) {
                let that = this;
                let record = that.tableData[index];
                if (record.acay0006 == '1') {
                    this.$Message.error("已经生成两节救助数据，不允许删除！");
                    return;
                }
                that.$Modal.confirm({
                    title: '确定删除吗？',
                    onOk: () => {
                        that.Loading = true;
                        let url = that.url.delete;
                        let dataParam = {};
                        dataParam.id = record.id;
                        postAction(url, dataParam).then((res) => {
                            if (res.data.errcode != 0) {
                                that.$Message.error(res.data.errmsg)
                            } else {
                                that.$Message.success(res.data.data.msg);
                                that.loadData(1)
                            }
                            that.Loading = false;
                        });
                    },
                    onCancel: () => {
                    }
                });

            },
            handleEdit(index) {
                let that = this;
                that.title = "专项救助标准编辑"
                let record = that.tableData[index];
                if (record.id) {
                    if (record.acay0006 == '1') {
                        this.$Message.error("已经生成两节救助数据，不允许编辑！");
                        return;
                    }
                    that.visible = true
                    that.showSubmit = true
                    that.formData = Object.assign({}, record)
                    that.formData.acay0001 = new Date(record.acay0001);
                    that.formData.acay0004 = new Date(record.acay0004);
                } else {
                    that.visible = false
                }
            },
            handleView(index) {
                let that = this;
                that.title = "专项救助标准查看"
                let record = that.tableData[index];
                if (record.id) {
                    that.visible = true
                    that.showSubmit = false
                    that.formData = Object.assign({}, record)
                    that.formData.acay0001 = new Date(record.acay0001);
                    that.formData.acay0004 = new Date(record.acay0004);
                } else {
                    that.visible = false
                }
            },
            handleSubmit(name) {
                this.$refs[name].validate((valid) => {
                    if (valid) {
                        let that = this;
                        let dataParam = {
                            id: that.formData.id,
                            acay0002: that.formData.acay0002,
                            acay0001: DateFormat(that.formData.acay0001, 'YYYY-MM'),
                            acay0005: that.formData.acay0005,
                            acay0010: that.formData.acay0010,
                            acay0004: DateFormat(that.formData.acay0004, 'YYYY-MM-DD'),
                        }
                        let url = null;
                        if (dataParam.id == null) {
                            url = that.url.add
                        } else {
                            url = that.url.edit
                        }
                        postAction(url, dataParam).then((res) => {
                            if (res.data.errcode != 0) {
                                that.$Message.error(res.data.data.msg)
                            } else {
                                that.$Message.success(res.data.data.msg);
                                that.visible = false
                                that.loadData(1)
                            }
                        }).finally(() => {
                        })
                    } else {
                        this.$Notice.error({title: '必填信息未填写完成!', desc: '必填信息未填写完成!'})
                    }
                });
            }
        },
        created() {
            this.loadData();
        },
    }
</script>
<style scoped>
    .paging {
        float: right;
        margin-top: 10px;
    }

    /deep/ .ivu-upload-list {
        display: none;
    }
</style>
